import Taro from '@tarojs/taro';

const host = 'http://localhost:8090';
// const host = 'http://8.130.123.79:8090';

export const getData = (url, data) => {
    return new Promise((resolve, reject) => {
        Taro.request({
            url: host + url,
            data, 
            method: 'GET', 
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
                resolve(res.data);
            },
            fail: function (res) {
                reject(res);
            }
        });
    });
};

export const deleteData = (url, data) => {
    return new Promise((resolve, reject) => {
        Taro.request({
            url: host + url,
            data, 
            method: 'DELETE', 
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
                resolve(res.data);
            },
            fail: function (res) {
                reject(res);
            }
        });
    });
};

export const postData = (url, data) => {
    return new Promise((resolve, reject) => {
        Taro.request({
            url: host + url, 
            data: data, 
            method: 'POST' || 'PUT', 
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                resolve(res.data);
            },
            fail: function (res) {
                reject(res);
            }
        });
    });
};

export const putData = (url, data) => {
    return new Promise((resolve, reject) => {
        Taro.request({
            url: host + url, 
            data: data, 
            method: 'PUT', 
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                resolve(res.data);
            },
            fail: function (res) {
                reject(res);
            }
        });
    });
};

export const postFile = (url, data) => {
    return new Promise((resolve, reject) => {
        Taro.request({
            url: host + url, 
            data: data, 
            method: 'POST' || 'PUT', 
            header: {
                'content-type': 'multipart/form-data'
            },
            success: function (res) {
                resolve(res.data);
            },
            fail: function (res) {
                reject(res);
            }
        });
    });
};
